/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entities.Vacancy;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;


/**
 *
 * @author hungxp
 */
public class createVacancyDao {
    ConnectDB myConn=new ConnectDB();
    Connection conn;
    public String layMaCuoiVacancy(){
        String mcv=null;
        String qr="select top 1 VacancyID from Vacancy ";
        try {
            conn=myConn.getConnect();
            Statement stm=conn.createStatement();
            ResultSet rs=stm.executeQuery(qr);
            while(rs.next()){
                mcv=rs.getString(1);
            }
        } catch (Exception e) {
        }
        return mcv;
    }
    public boolean createVacancy(Vacancy vca){
        Random rd = new Random(); 
        String ma=layMaCuoiVacancy();
        int maSo=Integer.parseInt(ma.substring(1, 3));
        String maNew="C"+(maSo+rd.nextInt(10000));
        String qr="insert into Vacancy values('"+ maNew+"','"+vca.getCategoryId() +"',"+
                    vca.getInforId()+","+vca.getCompanyId()+",N'"+vca.getTitle()+"',N'"+vca.getPosition()+
                    "',N'"+ vca.getWorkType()+"','"+vca.getCreateDate()+"','"+vca.getNumberRequirement()+
                    "',"+vca.getSalary() +",N'"+vca.getDescription()+"',N'"+vca.getEntitlement()+"','"+
                    vca.getYearOfExperience()+"',N'"+vca.getSkillRequirement()+"',N'"+vca.getDegree()+"',N'"+
                    vca.getGender()+"',N'"+vca.getRequirementAge()+"',N'"+vca.getProfile()+"','"+vca.getDeadline()+
                    "',N'"+vca.getSubmissionforms()+"',"+vca.getStatus()+")";
        try {
            conn=myConn.getConnect();
            PreparedStatement sta=conn.prepareStatement(qr);
            int rs= sta.executeUpdate();
            if(rs>0){
                return true;
            }
            else{
                return false;
            }
        } catch (Exception e) {
            System.out.println("loi"+e.getMessage());
            return false;
            
        }
    }
}
